// pages/detail/detail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 作品信息
    obj: {},
    // 评论列表
    comment: [],
    // 输入框是否聚焦
    inputfocus: false,
    // 输入框的值
    inputvalue: '',
    // 回复评论id
    commentId: '',
    // 分享弹窗
    showShare: false,
    options: [{
        name: '微信',
        icon: 'wechat',
        openType: 'share'
      },
      {
        name: '微博',
        icon: 'weibo'
      },
      {
        name: '复制链接',
        icon: 'link'
      },
      {
        name: '分享海报',
        icon: 'poster'
      },
      {
        name: '二维码',
        icon: 'qrcode'
      }
    ]
  },
  // 输入框失去焦点时
  handleblur: function () {
    this.setData({
      inputfocus: false
    })
  },
  // 点击输入
  handleclick: function () {
    this.setData({
      inputfocus: true
    })
  },
  // 获取输入框内容
  handleinput: function (e) {
    // console.log(e.detail.value)
    this.setData({
      inputvalue: e.detail.value
    })
  },
  // 点击发布
  handlesubmit() {
    let that = this
    if (this.data.inputvalue) {
      if (this.data.commentId) {
        let replyObj = {
          userid: wx.getStorageSync('userid'),
          comments: this.data.inputvalue,
          return: this.data.commentId
        };
        wx.request({
          url: `http://localhost:1234/forum/detail/reply?workid=${this.options.id}`,
          method: "POST",
          data: replyObj,
          success: res => {
            that.comm()
            that.handleblur()
            console.log(res.data)
            if (res.data == 'ok') {
              this.setData({
                commentId: ''
              })
            }
          }
        })
      } else {
        let commentObj = {
          userid: wx.getStorageSync('userid'),
          comments: this.data.inputvalue
        }
        // console.log(commentObj)
        wx.request({
          url: `http://localhost:1234/forum/detail/comment?workid=${this.options.id}`,
          method: "POST",
          data: commentObj,
          success: res => {
            console.log(res.data)
            that.comm()
            that.handleblur()
            if (res.data == 'ok') {}
          }
        })
      }
      this.setData({
        inputvalue: ''
      })
    }
    console.log(this.data.comment)
  },
  // 回复哪条评论,获取其commentid
  handlereply: function (e) {
    this.setData({
      inputfocus: true
    })
    // console.log(e.currentTarget.dataset.id)
    this.setData({
      commentId: e.currentTarget.dataset.id
    })
    // console.log(this.data.commentId)
  },
  // 点赞
  handlelike: function (e) {
    let workid = e.currentTarget.dataset.id;
    // console.log(workid)
    let userid = wx.getStorageSync('userid');
    wx.request({
      url: `http://localhost:1234/forum/like?workid=${workid}&userid=${userid}`,
      success: res => {
        // console.log(res)
        // 重新加载页面
        this.onShow()
      }
    })
  },
  // 收藏
  handlecollect: function (e) {
    let workid = e.currentTarget.dataset.id;
    // console.log(workid)
    let userid = wx.getStorageSync('userid');
    wx.request({
      url: `http://localhost:1234/forum/collect?workid=${workid}&userid=${userid}`,
      success: res => {
        // console.log(res)
        // 重新加载页面
        this.onShow()
      }
    })
  },
  // 分享
  handleshare: function () {
    this.setData({
      isShow: true
    })
    // console.log(this.data.isShow)
  },
  onSelect(event) {
    Toast(event.detail.name);
    this.onClose();
  },
  onClose() {
    this.setData({
      isShow: false
    });
  },
  // 关注/取关作者
  handleconcern() {
    let that = this
    wx.request({
      url: `http://localhost:1234/hu?type=3`,
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        'userid': wx.getStorageSync('userid'),
        'concernid': that.data.obj.userid
      },
      method: 'POST',
      success: function (res) {
        let status = res.data.status;
        if (status == 200) {
          that.setData({
            obj: {
              ...that.data.obj,
              user: {
                ...that.data.obj.user,
                isConcerned: !that.data.obj.user.isConcerned
              }
            }
          })
        }
      },
      fail: function () {
        console.log("加载失败")
      }
    })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 关注or没关注
    let that = this
    wx.request({
      url: `http://localhost:1234/fan?type=3`,
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        'userid': wx.getStorageSync('userid'),
        'concernid': that.data.obj.userid
      },
      method: 'POST',
      success: function (res) {
        let status = res.data.status;
        console.log(res)
        if (status == 200) {
          that.setData({
            obj: {
              ...that.data.obj,
              user: {
                ...that.data.obj.user,
                isConcerned: JSON.parse(res.data.hu)
              }
            }
          })
        }
      },
      fail: function () {
        console.log("加载失败")
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    console.log("workid:" + this.options.id)
    // 根据获取的id，向后端发送请求，获得详情数据
    wx.request({
      url: `http://localhost:1234/forum/detail?workid=${this.options.id}`,
      success: res => {
        console.log(res.data)
        let a = false
        if (res.data.userid == wx.getStorageSync('userid')) {
          a = true
        }
        this.setData({
          obj: res.data,
          user: a
        })
      }
    })
    // 获取评论列表
    wx.request({
      url: `http://localhost:1234/forum/detail/comment?workid=${this.options.id}`,
      success: res => {
        console.log(res.data)
        this.setData({
          comment: res.data
        })
      }
    })
  },
  navi() {
    let that = this
    wx.navigateTo({
      url: '/pages/user/user?id=' + that.data.obj.userid,
    })
  },
  comm() {
    wx.request({
      url: `http://localhost:1234/forum/detail/comment?workid=${this.options.id}`,
      success: res => {
        console.log(res.data)
        this.setData({
          comment: res.data
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  // 若是我发布的作品,可以删除
  deletework() {
    wx.showModal({
      title: '提示',
      content: '确定删除该作品吗?',
      success: (res) => {
        if (res.confirm) {
          wx.showToast({
            title: '成功',
            icon: 'success',
            duration: 2000 //持续的时间
          })
          wx.request({
            url: `http://localhost:1234/users/detail?workid=${this.options.id}`,
            success: res => {
              wx.switchTab({
                url: '/pages/person/person',
              })
            }
          })
        }
      }
    })
  }
})